package com.etone.H2;

import java.io.BufferedReader;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import org.h2.tools.Csv;
import org.h2.tools.SimpleResultSet;

import com.etone.H2.engine.DataServer;

public class H2Function {

	static BufferedReader reader;
	static ResultSet rs = null;
	static SimpleResultSet rs2 = null;
	
	
	public static SimpleResultSet getRs2() {
		
		while(rs2 == null){
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return rs2;
	}

	static class  GetResultSet extends Thread{
		
		public void run(){

			if(null == rs){
		    	try {
//					reader = new BufferedReader(new FileReader(new File("F:/10K.csv")));

		    		int i = 0;
//		    		while(reader.ready()){
//		    			
//		    			System.out.println(++i + " " + reader.readLine());
//		    		}
		    		
					
//					rs = Csv.getInstance().read("F:/10K.csv", Export2Csv.ColumnNames, "UTF-8");
					rs = Csv.getInstance().read(reader, Export2Csv.ColumnNames);
					
					int count = rs.getMetaData().getColumnCount();
					System.out.println(count);
					
					rs.next();
					while(rs.next()){
						Object columns[] = new String[rs.getMetaData().getColumnCount()];
						for(int k=1;k<=count;k++){
							if(rs2 == null){
								rs2 = new SimpleResultSet();
								rs2.addColumn(rs.getMetaData().getColumnName(k),
										rs.getMetaData().getColumnType(k), 
										rs.getMetaData().getPrecision(k),
										rs.getMetaData().getScale(k)
										);
							}
							
							
							System.out.print("\"" + rs.getString(k)+ "\",");
							columns[k-1] = rs.getString(k);
						}
						System.out.println("\n" +rs.getMetaData().getPrecision(26)+ " : " +rs.getMetaData().getPrecision(26)+" : "+ ++i + " : " + columns.length + " : " + rs.getMetaData().getColumnType(4) ); 
						rs2.addRow(columns);
						
					}
					
					
					System.out.println("query end");
				} catch (Exception e) {
					e.printStackTrace();
				}
		    	
			}
			
		
		}
	}
	
	
	static{
		DataServer.main(null);
		reader = DataServer.getReader();
	}
	
	
    public static ResultSet queryFromReader() {
    	
    	new GetResultSet().start();
    	ResultSet rs = getRs2();
    	try {
			int count = rs.getMetaData().getColumnCount();
			while (rs.next()) {
				for (int k = 1; k < count; k++) {

					System.out.print("\"" + rs.getObject(k) + "\",");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rs;
    }
    
    public static void registerQueryFromReader(Connection conn){
    	try {
    		conn.createStatement().execute("CREATE ALIAS CSV_Read FOR \"com.etone.H2.H2Function.queryFromReader\" ");
		} catch (SQLException e) {
			e.printStackTrace();
		}
    }
    
    public static void main(String[] args) {
    	H2Function.queryFromReader();
	}
}
